#!/usr/bin/python3
"""Upotreba ./validate.py test/*.in*"""

import string


def check(lines):
    nl = []   # ispravno formatirane linije

    n = int(lines[0])
    assert 1 <= n <= 1439, "n kriv"
    nl.append("{}\n".format(n))
    last = 0
    for i in range(n):
        mi, ti = map(int, lines[i+1].split())
        assert 1 <= mi <= 1440, "m kriv"
        assert -40 <= ti <= 40, "t kriv"
        nl.append("{} {}\n".format(mi, ti))
        assert last <= mi, "Minute nisu od manje prema vecoj"
        last = mi

    x = int(lines[n+1])
    assert 1 <= x <= 1440, "kriv x"
    nl.append("{}\n".format(x))

    lower = [0, 0]
    best = 10 ** 10, -1
    for i in range(n):
        mi, ti = map(int, lines[i+1].split())
        if ti < x: lower = mi, ti
        if abs(ti - x) < abs(x - best[1]):
            best = mi, ti

    assert lines == nl, "Krivi format (%s vs %s)" % (lines, nl)
    assert lines[-1][-1] == "\n", "Zadnji red ne zavrsava sa \\n"
    return n, lower[0] == best[0]


# Ocekivani clusteri! Ovo vjerojatno zelis promijeniti!
expected_clusters = {'n=5_prva_manja': 4, 'n=5': 8, 'prva_manja': 1, 'normalni': 7}

def what_cluster(data):
    n, lower_correct = data
    # na temelju povratne informacije iz check(lines)
    # zakljucuje za TP u kojoj je bodovnoj sekciji
    if n == 5: 
        if lower_correct: return "n=5_prva_manja"
        return 'n=5'
    if lower_correct: return "prva_manja"
    return 'normalni'


################### Zadatak-specifican kod iznad ove linije #########################

import sys
import glob
import hashlib


def group_in_batches(files):
    # mnozenje.in.1a, mnozenje.in.1b sprema u isti batch

    files.sort()
    B = []
    for f in files:
        if f[-1].islower() and len(B) > 0 and f[:-1] == B[-1][-1][:-1]:
            B[-1].append(f)
        else:
            B.append([f])
    return B


if __name__ == "__main__":
    f = []
    for pattern in sys.argv[1:]:
        for filename in glob.glob(pattern):
            f.append(filename)

    bc = []
    for batch in group_in_batches(f):
        if 'dummy' not in batch[0]:
            bc.append([])
        for filename in batch:
            print("{}: ".format(filename), end="")
            try:
                lines = open(filename).readlines()
                summary = check(lines)
                c = what_cluster(summary)
                if 'dummy' not in batch[0]:
                    bc[-1].append(c)
            except Exception as e:
                print("Greska!", e)
                raise
            else:
                print("Sve ok! (cluster {}, summary = {})".format(c, summary))

    clusters = {}
    for b in bc:
        for c in b:
            assert c == b[0], "Ima razlicitih cluster-a unutar batcha"
        if not b[0] in clusters:
            clusters[b[0]] = 0
        clusters[b[0]] += 1

    assert clusters == expected_clusters, "Kriva raspodjela clustera ({} vs {})".format(clusters, expected_clusters)

    # Buda test - provjeri duplikate
    hashes = set(hashlib.sha1(open(x, 'rb').read()).hexdigest() for x in f)
    assert len(hashes) == len(f), "Ima duplikata!"
